package com.bdqn.bookshop.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.bookshop.common.BookConstants;
import com.bdqn.bookshop.dao.NoticeMapper;
import com.bdqn.bookshop.entity.AdminUser;
import com.bdqn.bookshop.entity.Notice;
import com.bdqn.bookshop.service.NoticeService;
import com.bdqn.bookshop.utils.DataGridViewResult;
import com.bdqn.bookshop.utils.Result;
import com.bdqn.bookshop.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.ast.Not;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author : 罗若然[LUO_RUO_RAN]
 * @date : 2020-07-24 16:02
 **/
@Service
@Transactional
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {


    @Override
    public Result addNotice(Notice notice, HttpServletRequest request) {
        notice.setChangeTime(new Date());
        notice.setCreateTime(new Date());
        AdminUser user = (AdminUser) request.getSession().getAttribute(BookConstants.LOGIN_ADMIN_USER);
        notice.setPublisher(user.getId());
        notice.setVisit(0);
        if(baseMapper.insert(notice)>0){
            return Result.createSuccess("发布成功");
        }
        return Result.createError("发布失败");
    }

    @Override
    public Result updateNotice(Notice notice) {
        notice.setChangeTime(new Date());
        if(baseMapper.updateById(notice)>0){
            return Result.createSuccess("修改成功");
        }
        return Result.createError("修改失败");
    }

    @Override
    public Result deleteById(Integer id) {
        if(baseMapper.deleteById(id)>0){
            return Result.createSuccess("删除成功");
        }
        return Result.createError("删除失败");
    }

    @Override
    public Result batchDelete(String ids) {
        String [] idArr = ids.split(",");
        if (baseMapper.deleteBatchIds(Arrays.asList(idArr))>0){
            return  Result.createSuccess("批量删除成功");
        }
        return Result.createError("批量删除失败");
    }

    @Override
    public DataGridViewResult noticeList(NoticeVo noticeVo) {
        Page<Notice> page = new Page<Notice>(noticeVo.getPage(),noticeVo.getLimit());
        IPage<Notice> page1 = baseMapper.selectNoice(page,noticeVo);
        return new DataGridViewResult(page1.getTotal(),page1.getRecords());
    }

    @Override
    public List<String> lifePeriod() {
        return  baseMapper.selectNoiceTime();
    }

    @Override
    public List<Notice> newNotice() {
        IPage<Notice> page = new Page<>(1,5);
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        baseMapper.selectPage(page,queryWrapper);
        return page.getRecords();
    }

    @Override
    public Result getNoticeCount(String date) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(date),"create_time",date);
        Integer count = baseMapper.selectCount(queryWrapper);
        return Result.createSuccess(count,"有数据");
    }

    @Override
    public List<Notice> getNoticePage(NoticeVo noticeVo, String date) {
        IPage<Notice> page = new Page<>(noticeVo.getPage(),noticeVo.getLimit());
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(date),"create_time",date);
        queryWrapper.orderByDesc("create_time");
        baseMapper.selectPage(page,queryWrapper);
        return page.getRecords();
    }

    @Override
    public Notice getNoticeAndUpdateVisit(Integer id) {
        Notice notice = baseMapper.selectById(id);
        notice.setVisit(notice.getVisit()+1);
        if(baseMapper.updateById(notice)>0){
            return notice;
        }
        return null;
    }

}
